package net.libra.ui.layer
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;

	/**
	 * @author elvisjiang
	 */	
	public class LayerManager
	{
		public function LayerManager()
		{
		}
		
		static public var parent:DisplayObjectContainer;
		static protected var width:Number;
		static protected var height:Number;
		static protected var defaultLayer:String;
		static public function setContainer(parent:DisplayObjectContainer,width:Number = NaN,height:Number = NaN):void
		{
			LayerManager.parent = parent;
			LayerManager.width = width;
			LayerManager.height = height;
		}
		static public function getStage():Stage
		{
			return parent.stage;
		}
		
		
		static public function setDefaultLayer(name:String):void
		{
			defaultLayer = name;
		}
		static public function showLayer(name:String):void
		{
			var layer:Sprite = getLayer(name);
			if(layer)
			{
				layer.x = 0;
			}
		}
		static public function hideLayer(name:String):void
		{
			var layer:Sprite = getLayer(name);
			if(layer)
			{
				layer.x = 9999;
			}
		}
		
		
		static public function addLayer(name:String,mouseEnabled:Boolean):void
		{
			var disp:DisplayObject = parent.getChildByName(name);
			if(disp)
			{
				trace("layer <"+name+"> 已创建!");
				return;
			}
			var layer:Sprite = new Sprite();
			layer.mouseChildren = layer.mouseEnabled = mouseEnabled;
			layer.name = name;
			parent.addChild(layer);
		}
		static public function getLayer(name:String):Sprite
		{
			if(!name)
			{
				return null;
			}
			return parent.getChildByName(name) as Sprite;
		}
		
		static public function addChild(child:DisplayObject,layer:String=null):DisplayObject
		{
			var sp:Sprite = getLayer(layer||defaultLayer);
			if(sp)
			{
				sp.addChild(child);
			}
			return child;
		}

		static public function addChildAt(child:DisplayObject,index:int,layer:String=null):DisplayObject
		{
			var sp:Sprite = getLayer(layer||defaultLayer);
			if(sp)
			{
				sp.addChildAt(child,index);
			}
			return child;
		}
		
		static public function getChildByName(name:String,layer:String=null):DisplayObject
		{
			var sp:Sprite;
			if(layer)
			{
				sp = getLayer(layer);
				if(sp)
				{
					return sp.getChildByName(name);
				}
				return null;
			}
			
			for(var i:uint=0;i<parent.numChildren;i++)
			{
				sp = parent.getChildAt(0) as Sprite;
				if(sp)
				{
					var child:DisplayObject = sp.getChildByName(name);
					if(child)
					{
						return child;
					}
				}
			}
			return null;
		}
		
		static public function removeChild(child:DisplayObject):DisplayObject
		{
			if(!child)
			{
				return child;
			}
			if(child.parent)
			{
				child.parent.removeChild(child);
			}
			return child;
		}
		
		static public function setCenter(child:DisplayObject):DisplayObject
		{
			if(!child)
			{
				return child;
			}
			child.x = int((getWidth() - child.width)/2);
			child.y = int((getHeight() - child.height)/2);
			return child;
		}
		
		static public function getWidth():Number
		{
			return isNaN(width)?getStage().stageWidth:width;
		}
		static public function getHeight():Number
		{
			return isNaN(height)?getStage().stageHeight:height;
		}
	}
}